package com.shuorikeji.huohuo.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.shuorikeji.huohuo.R;
import com.shuorikeji.huohuo.bean.DjDetailVideoBean;
import com.shuorikeji.huohuo.callback.ClickCallBack;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

public class DjJsAdapter extends RecyclerView.Adapter<DjJsAdapter.BaseViewHolder> {
    private final Context mContext;
    private List<DjDetailVideoBean> mData = new ArrayList<>();
    private ClickCallBack callBack;

    public DjJsAdapter(Context context, ClickCallBack callBackFlag) {
        this.mContext = context;
        this.callBack = callBackFlag;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.dj_jj_item_layout, parent, false);
        BaseViewHolder holder = new BaseViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.tvJs.setText(String.valueOf(position + 1));
        holder.ivPlaying.setVisibility((mData.get(position).isPlaying()) ? View.VISIBLE : View.GONE);
        holder.ivSuo.setVisibility((mData.get(position).getIs_lock() == 2) ? View.GONE : View.VISIBLE);
        if (mData.get(position).getIs_lock() == 2) {
            holder.tvJs.setTextColor((mData.get(position).isPlaying()) ? mContext.getResources().getColor(R.color.color_EE591D) :
                    mContext.getResources().getColor(R.color.color_151718));
        } else {
            holder.tvJs.setTextColor(mContext.getResources().getColor(R.color.color_858C96));
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (callBack != null) {
                    callBack.onClick(1, mData.get(position));
                }
            }
        });
    }

    public void setData(List<DjDetailVideoBean> data) {
        if (mData != null) {
            mData.clear();
            mData.addAll(data);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class BaseViewHolder extends RecyclerView.ViewHolder {
        TextView tvJs;
        ImageView ivPlaying;
        ImageView ivSuo;

        public BaseViewHolder(View itemView) {
            super(itemView);
            tvJs = itemView.findViewById(R.id.tv_js);
            ivPlaying = itemView.findViewById(R.id.iv_playing);
            ivSuo = itemView.findViewById(R.id.iv_suo);
        }
    }
}
